#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_data_sync.sh
##  Functions : 
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo -e "\nHow to use this shell script!"
    echo -e '\n${V_SHELL_HOME}/edw_data_sync.sh A'
}

# Check variable
V_GROUP=${1}
if [[ -z ${V_GROUP} ]];then
    USAGE
    exit 1
fi

# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_SCRIPT_NAME}_${V_GROUP}_${V_TIME_STAMP}_$$.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output
echo -e "[Begin: $(date +%F) $(date +%T)]"

echo -e "\nReady to sync group ${V_GROUP} data..."

# Get data date
V_DATA_DATE=$(grep V_DATA_DATE "${V_SHELL_PARM}/ALL_ORG/edw_parm.txt" | awk -F'=' '{print $2}')
V_LAST_MONTH_END_DATE=$(grep V_LAST_MONTH_END_DATE "${V_SHELL_PARM}/ALL_ORG/edw_parm.txt" | awk -F'=' '{print $2}')

echo -e "\n[------------------------Start to export data------------------------]"
V_EXPORT=(mysql hive)
for ((i=0; i<${#V_EXPORT[@]}; i++));do
    echo "$(date)"
    sh ${V_PARALL_EXPT_DATA} ${V_DATA_DATE} ${V_GROUP} ${V_EXPORT[i]} 
    echo -e "$(date) Call[${!}]:${V_PARALL_EXPT_DATA} ${V_EXPORT[i]}"
    V_EXPORT_PID=(${V_EXPORT_PID} ${!})
done


echo -e "\n[------------------------Start to import data------------------------]"
V_IMPORT=(hive mysql)
for ((i=0; i<${#V_IMPORT[@]}; i++));do
   echo "$(date)"
    sh ${V_PARALL_IMPT_DATA} ${V_DATA_DATE} ${V_GROUP} ${V_IMPORT[i]} 
    echo -e "$(date) Call[${!}]:${V_PARALL_IMPT_DATA} ${V_IMPORT[i]}"
    V_IMPORT_PID=(${V_IMPORT_PID} ${!})
done

# Check the parm PID
echo -e "\nStarting to check export data..."
for ((i=0; i<${#V_EXPORT_PID[@]}; i++));do
    # Get the return code
    wait ${V_EXPORT_PID[i]}
    if [[ $? -eq 0 ]];then
        echo -e "${V_EXPORT_PID[i]} has done."
    else
        x=fail
        echo -e "ERROR[${V_EXPORT_PID[i]}]:Fail to export data."
    fi
done

# Check the parm PID
echo -e "\nStarting to check import data..."
for ((i=0; i<${#V_IMPORT_PID[@]}; i++));do
    # Get the return code
    wait ${V_IMPORT_PID[i]}
    if [[ $? -eq 0 ]];then
        echo -e "${V_IMPORT_PID[i]} has done."
    else
        x=fail
        echo -e "ERROR[${V_IMPORT_PID[i]}]:Fail to import data."
    fi
done

# Check all
if [[ ${x} == "fail" ]];then
    exit 1
fi

echo -e "\n[End: $(date +%F) $(date +%T)]"
echo 0 >&4
exit 0
